<?php

use yii\db\Migration;
use yii\db\Schema;
/**
 * Handles the creation of table `my_module`.
 */
class m170707_012303_create_my_module_table extends Migration
{
    /**
     * @inheritdoc
     */
    public function up()
    {
        $tableOptions = null;
        $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB';



        /**
         * 大字注意  使用mysql外键的时候  参照键必须为主键或者索引    并且两个键数据类型必须必须一致 重点 ：：： 无符号
         * 还有外键删除时的SET null 与 not null之冲突
         *
         * module type 表
         * 类型
         * 1 ： 纯内容模块   eg: 某种介绍/描述      table content
         * 2 :  showImg  展示型活动图片模块  eg 图片+链接+simple des   table active_img
         * 3 :  常态adv  固定展示 模块       eg banner                table banner
         * 4 ：  active 动态活动模块         eg active                table active 可与其他模块关联        不可以作为父模块
         * 5 :  文章列表模块
         * 6 :  表格类
         * 7 :  画册膜哈
         */
        $this->createTable('{{%module_type}}',[
            'id'=>Schema::TYPE_PK,
            'name'=>$this->string(64)->notNull()->defaultValue(''),
            'describtion' => $this->string(140)->notNull()->defaultValue(''),
            'table_name' => $this->string(64)->notNull()->defaultValue(''),
            'created_at' => $this->integer(),
            'updated_at' => $this->integer(),
            'isUse' => $this->boolean()->notNull()->defaultValue(1),
        ],$tableOptions);


        /**
         * module表
         */
        $this->createTable('{{%modules}}',[
            'id'=> Schema::TYPE_PK,
            'type'=>$this->integer(10)->notNull(),                      //模块类型 外键
            'name'=>$this->string(64)->notNull()->unsigned()->defaultValue(''),
            'sort'=>$this->integer(10)->notNull()->defaultValue(0),
            'isShow'=>$this->boolean()->defaultValue(0),
            'describtion' => $this->string(140)->notNull()->unsigned()->defaultValue(''),
            'content' => $this->text()->notNull()->unsigned()->defaultValue(''),
            'created_at' => $this->integer(),
            'updated_at' => $this->integer()
            ],$tableOptions);
        $this->addForeignKey('fk_modules_type','modules','type','module_type','id','CASCADE');

        /**
         * module special  特定module表
         * content type  纯内容模块  eg : 某种介绍/描述
         */
        $this->createTable('{{%content_module}}',[
            'id'=>Schema::TYPE_PK,
            'moduleid'=>$this->integer(10)->notNull(),
            'name'=>$this->string(64)->notNull()->unsigned()->defaultValue(''),
            'describtion' => $this->string(140)->notNull()->unsigned()->defaultValue(''),
            'content' => $this->text()->notNull()->unsigned()->defaultValue(''),
            'created_at'=>$this->integer(),
            'updated_at'=>$this->integer(),
            'author' => $this->string(16)->notNull()->unsigned()->defaultValue(''),                           //作者id
            'provider' => $this->string(32)->notNull()->unsigned()->defaultValue('')                         //提供方id
        ],$tableOptions);

        $this->addForeignKey('fk_content_module_moduleid','content_module','moduleid','modules','id','CASCADE');

        /**
         * module special
         * showImg type 展示型模块  用于活动 或者 公告
         */
        $this->createTable('{{%showimg_module}}',[
            'id' => Schema::TYPE_PK,
            'moduleid'=>$this->integer(10)->notNull(),
            'name'=>$this->string(32)->notNull()->unsigned()->defaultValue(''),
            'img' =>$this->string(255)->notNull()->unsigned()->defaultValue(''),                              //图片路径 多张图片以 |相隔
            'link'=>$this->string(255)->notNull()->defaultValue(''),            //链接地址 如为外链则填写改链接
            'active'=>$this->string(64)->notNull()->unsigned()->defaultValue(''),                             //动态，模块id  以 | 分隔
            'describtion'=>$this->string(140)->notNull()->unsigned()->defaultValue(''),
            'helper'=> $this->text()->notNull()->unsigned()->defaultValue(''),
            'showContent'=>$this->boolean()->defaultValue(0),
            'created_at'=>$this->integer(),
            'updated_at'=>$this->integer(),
            'author'=>$this->string(16)->notNull()->unsigned()->defaultValue(''),
            'provider'=>$this->string(32)->notNull()->unsigned()->defaultValue('')
        ],$tableOptions);

        $this->addForeignKey('fk_showimg_module_moduleid','showimg_module','moduleid','modules','id','CASCADE');

        /**
         * module adv always
         */
        $this->createTable('{{%advs_module}}',[
            'id' => Schema::TYPE_PK,
            'moduleid'=>$this->integer(10)->notNull(),
            'name'=>$this->string(32)->notNull(),
            'table'=>$this->string(32)->notNull(),
            'created_at'=>$this->integer(),
            'updated_at'=>$this->integer()
        ],$tableOptions);

        $this->addForeignKey('fk_advs_module_moduleid','advs_module','moduleid','modules','id','CASCADE');


        //与 advsModule  关联
        $this->createTable('{{%banner}}',[
            'id'=>Schema::TYPE_PK,
            'advid'=>$this->integer(10)->notNull(),
            'img'=>$this->string(255)->notNull()->unsigned()->defaultValue(''),                           //图片路径 以|分隔
            'link'=>$this->string(255)->notNull()->unsigned()->defaultValue(''),                          //外链地址  以 | 分隔
            'active'=>$this->string(64)->notNull()->unsigned()->defaultValue('')            //动态模块id 以 | 分隔
        ],$tableOptions);

        $this->addForeignKey('fk_banner_advid','banner','advid','advs_module','id','CASCADE');

        /**
         * active module 动态活动模板
         *创建一个隐式module
         */
        $this->createTable('{{%active_modules}}',[
            'id' => Schema::TYPE_PK,
            'moduleid'=>$this->integer(10)->notNull(),
            'type'=> $this->integer(10)->notNull(),                //type无法选择动态模块
            'created_at'=>$this->integer(),
            'updated_at'=>$this->integer()
        ],$tableOptions);

        $this->addForeignKey('fk_active_modules_moduleid','active_modules','moduleid','modules','id','CASCADE');
        $this->addForeignKey('fk_active_modules_type','active_modules','type','module_type','id','CASCADE');

        /**
         * 文章列表
         * article list module
         */
        $this->createTable('{{%arclist_module}}',[
            'id' => Schema::TYPE_PK,
            'name' => $this->string(32)->notNull()->unsigned()->defaultValue(''),
            'moduleid'=>$this->integer(10)->notNull(),
            'describtion' => $this->string(140)->notNull()->unsigned()->defaultValue(''),
            'parentid' => $this->integer(10)->notNull()->unsigned()->defaultValue(0),
            'isparent'=>$this->boolean()->defaultValue(0),                  //本身有子分类情况下 不予添加文章权限
            'created_at'=>$this->integer(),
            'updated_at'=>$this->integer(),
            'table' => $this->string(16)->notNull()->unsigned()->defaultValue('')
        ],$tableOptions);

        $this->addForeignKey('fk_arclist_module_moduleid','arclist_module','moduleid','modules','id','CASCADE');

        /**
         * 文章列表 下属 文章表
         * table article    [格式①]
         */
        $this->createTable('{{%article_tem1}}',[
            'id'=>Schema::TYPE_PK,
            'articleid'=>$this->integer(10)->notNull(),
            'title'=>$this->string(32)->notNull()->unsigned()->defaultValue(''),
            'describtion'=>$this->string(255)->notNull()->unsigned()->defaultValue(''),
            'content'=>$this->text()->notNull()->defaultValue('')->unsigned()->defaultValue(''),
            'author'=>$this->string(16)->notNull()->unsigned()->defaultValue(''),
            'provider'=>$this->string(16)->notNull()->unsigned()->defaultValue(''),
            'created_at'=>$this->integer(),
            'updated_at'=>$this->integer()
        ],$tableOptions);

        $this->addForeignKey('fk_article_tem1_articleid','article_tem1','articleid','arclist_module','id','CASCADE');

        /**
         * table module 表格类
         */
        $this->createTable('{{%table_module}}',[
            'id'=>Schema::TYPE_PK,
            'moduleid'=>$this->integer(10)->notNull(),
            'key'=>$this->string(32)->notNull()->unsigned()->defaultValue('')->defaultValue(''),
            'value'=>$this->string(255)->notNull()->unsigned()->defaultValue(''),
            'parentid'=>$this->integer(10)->notNull()->unsigned()->defaultValue(0),
            'describtion'=>$this->string(140)->unsigned()->defaultValue(''),
            'created_at'=>$this->integer(),
            'updated_at'=>$this->integer()
        ],$tableOptions);

        $this->addForeignKey('fk_table_module_moduleid','table_module','moduleid','modules','id','CASCADE');

        /**
         * 画册模块
         * table ScreenShot
         */
        $this->createTable('{{%pic_module}}',[
            'id'=>Schema::TYPE_PK,
            'moduleid'=>$this->integer(10)->notNull(),
            'title'=>$this->string(32)->notNull()->unsigned()->defaultValue(''),
            'link'=>$this->string(64)->notNull()->unsigned()->defaultValue(''),              //外链
            'resource'=>$this->string(32)->notNull()->unsigned()->defaultValue(''),          //与外链同用
            'parentid'=>$this->integer(10)->notNull()->unsigned()->defaultValue(0),
            'isparent'=>$this->boolean()->defaultValue(0),
            'created_at'=>$this->integer(),
            'updated_at'=>$this->integer()
        ],$tableOptions);

        $this->addForeignKey('fk_pic_module_moduleid','pic_module','moduleid','modules','id','CASCADE');

        /**
         * 图片数据表
         * table pictures
         */
        $this->createTable('{{%pictures}}',[
            'id'=>Schema::TYPE_PK,
            'picid' => $this->integer(10)->notNull(),
            'src'=>$this->string(32)->notNull()->unsigned()->defaultValue(''),
            'alt'=>$this->string(32)->notNull()->unsigned()->defaultValue(''),
            'describtion'=>$this->string(140)->notNull()->unsigned()->defaultValue(''),
            'created_at'=>$this->integer(),
            'updated_at'=>$this->integer()
        ],$tableOptions);

        $this->addForeignKey('fk_pictures_picid','pictures','picid','pic_module','id','CASCADE');


        /**
         *为auth_assignment 与 admin 表 添加关联
         */
    }

    /**
     * @inheritdoc
     */
    public function down()
    {
        $this->dropTable('modules');
        $this->dropTable('module_type');
        $this->dropTable('content_module');
        $this->dropTable('showimg_module');
        $this->dropTable('advs_module');
        $this->dropTable('banner');
        $this->dropTable('active_modules');
        $this->dropTable('arclist_module');
        $this->dropTable('article_tem1');
        $this->dropTable('table_module');
        $this->dropTable('pic_module');
        $this->dropTable('pictures');
    }


    protected function buildFkClause($delete = '',$update = ''){

        return implode(' ',array(' ',$delete,$update));

    }
}
